$(document).ready(function () {
    spzwcx.onLoad();
})

var spzwcx = (function () {
    var curSeg;
    var flag1 = true;
    var flag2 = true;
    var flag3 = true;
    var flag4 = true;
    var init = function () {
        $('#picCode1').attr("src", jsptHtp + '/web/cm/showPictureCode.do?t=' + Math.random());

        //完税证点击详情
        $('#wxlspzwcx1').on("click", ".btnEdit", function () {
            var rowIndex = $(this).attr('data-rowindex');
            var result = $('#wxlspzwcx1').DataTable().rows(rowIndex).data()[0];
            result.spType = 'wsz';
            parent.ggcx.openTab(result);
        })

        //车船税点击详情
        $('#wxlspzwcx3').on("click", ".btnEdit", function () {
            var rowIndex = $(this).attr('data-rowindex');
            var result = $('#wxlspzwcx3').DataTable().rows(rowIndex).data()[0];
            result.spType = 'ccs';
            parent.ggcx.openTab(result);
        })

        //契耕税票点击详情
        $('#wxlspzwcx4').on("click", ".btnEdit", function () {
            var rowIndex = $(this).attr('data-rowindex');
            var result = $('#wxlspzwcx4').DataTable().rows(rowIndex).data()[0];
            result.spType = 'qgs';
            parent.ggcx.openTab(result);
        })
    };
    return {
        onLoad: function () {

            init();
            curSeg = spzwcx;
            curSeg.onSwitch();
            curSeg.onLayui();
        },

        //加载layui
        onLayui: function () {
            layui.use('form', function () {
                var form = layui.form
            });
            layui.use('laydate', function () {
                var laydate = layui.laydate;
                laydate.render({
                    elem: '#wxl_date1',
                    theme: 'molv'
                });
            })
        },

        //点击切换
        onSwitch: function () {
            $(".wxl_spzwcx_tab span").click(function () {
                var n = $(this).index();
                $(this).addClass("active").siblings("span").removeClass("active");
                $(".wxl_spzwxc_list").eq(n).show().siblings(".wxl_spzwxc_list").hide();
                $('#picCode' + (n + 1)).attr("src", jsptHtp + '/web/cm/showPictureCode.do?t=' + Math.random());
                $('.wxl_table_box').hide();
                $('.wxl_table_box').eq(n).show();
                $(":input[type=text]").val('');
                $("#wxlspzwcx1 tbody").empty();
                $("#wxlspzwcx2 tbody").empty();
                $("#wxlspzwcx3 tbody").empty();
                $("#wxlspzwcx4 tbody").empty();
            })
        },

        winReset: function () {
            $(":input[type=text]").val('');
            $("#wxlspzwcx1 tbody").empty();
            $("#wxlspzwcx2 tbody").empty();
            $("#wxlspzwcx3 tbody").empty();
            $("#wxlspzwcx4 tbody").empty();
        },

        onQueryWSZ: function () {
            if (flag1 == false) {
                return false;
            }
            if (curSeg.inputIsEmpty('.wxn_wsz')) {

            } else {
                var NSRSBH = $(":input[name='wsz_NSRSBH']").val().trim();
                var DZSPHM = $(":input[name='wsz_DZSPHM']").val().trim();
                var SJJE = $(":input[name='wsz_SJJE']").val().trim();
                var rcode = $(":input[name='wsz_rcode']").val().trim();

                flag1 = false;

                $.ajax({
                    url: jsptHtp + "/web/server/platform/sscx/selWszQuery.do",
                    type: "GET",
                    data: {
                        NSRSBH: NSRSBH,
                        DZSPHM: DZSPHM,
                        SJJE: SJJE,
                        rcode: rcode,
                    },
                    dataType: "jsonp",
                    jsonpCallback: "spzwcx.onqueryWSZCallback",
                });
            }

        },

        onqueryWSZCallback: function (jsonObj) {
            curSeg.reloadPicCode(1);
            flag1 = true;
            switch (jsonObj.code) {
                case "0":
                    if ($.fn.dataTable.isDataTable('#wxlspzwcx1')) {
                        var table = $('#wxlspzwcx1').DataTable();
                        table.destroy();
                    }
                    $('#wxlspzwcx1').DataTable({
                        "data": jsonObj.data,
                        "columns": [{
                                "data": null,
                                "render": function (data, type, full, meta) {
                                    return meta.row + 1 + meta.settings._iDisplayStart;
                                }
                            }, {
                                "data": "NSRSBH"
                            },
                            {
                                "data": "NSRMC"
                            },
                            {
                                "data": "JDH"
                            },
                            {
                                "data": "PZLX"
                            },
                            {
                                "data": null,
                                "render": function (data, type, full, meta) {
                                    return "";
                                }
                            },
                            {
                                "data": "KPRQ",
                                "render": function (data, type, full, meta) {
                                    return data.substring(0, 10);
                                }
                            },
                            {
                                "data": "KPR"
                            },
                            {
                                "data": "SEHJ"
                            },
                            {
                                "data": null,
                                // "render": function (data, type, full, meta) {
                                //     return "<a onclick='spzwcx.openParentTab(\"wsz\",\"" + data.NSRSBH + "\",\"" + data.JDH + "\")' style='color:#1f3df7;text-decoration:underline;cursor:pointer;'>查看明细</a>";
                                // }
                                "render": function (data, type, full, meta) {
                                    return "<a class = \"btnEdit\" data - rowindex = \"" + meta.row + "\" style='color:#1f3df7;text-decoration:underline;cursor:pointer;'>查看明细</a>";
                                }



                            }
                        ],
                        "ordering": false,
                        "info": false,
                        "lengthChange": false,
                        "searching": false,
                        "paging": false
                    })
                    break;
                default:
                    layui.use('layer', function () {
                        var layer = layui.layer;
                        layer.msg(jsonObj.msg);
                    })
            }
        },

        onQueryGRWSZ: function () {
            if (flag2 == false) {
                return false;
            }
            if (curSeg.inputIsEmpty('.wxn_grwsz')) {

            } else {
                var SFZJHM = $(":input[name='grwsz_SFZJHM']").val().trim();
                var NSRMC = $(":input[name='grwsz_NSRMC']").val().trim();
                var KJJE = $(":input[name='grwsz_KJJE']").val().trim();
                var rcode = $(":input[name='wsz_rcode']").val().trim();
                var PZHM = $(":input[name='grwsz_PZHM']").val().trim();
                var PZZG_DM = $('.grwsz_PZZG_DM').find('input').val();
                var SFZJHM_TYPE;
                switch ($('.grwsz_SFZJLX_DM').find('input').val()) {
                    case "居民身份证":
                        SFZJHM_TYPE = "201";
                        break;
                    case "军官证":
                        SFZJHM_TYPE = "202";
                        break;
                    case "士兵证":
                        SFZJHM_TYPE = "204";
                        break;
                    case "外国护照":
                        SFZJHM_TYPE = "208";
                        break;
                    case "港澳居民来往内地通行证":
                        SFZJHM_TYPE = "210";
                        break;
                    case "台湾居民来往大陆通行证":
                        SFZJHM_TYPE = "213";
                        break;
                    default:
                        SFZJHM_TYPE = "227"
                }

                flag2 = false;

                $.ajax({
                    url: jsptHtp + "/web/server/platform/sscx/selGswszQuery.do",
                    type: "GET",
                    data: {
                        SFZJHM: SFZJHM,
                        NSRMC: NSRMC,
                        KJJE: KJJE,
                        rcode: rcode,
                        PZZG_DM: PZZG_DM,
                        SFZJLX_DM: SFZJHM_TYPE,
                        PZHM: PZHM
                    },
                    dataType: "jsonp",
                    jsonpCallback: "spzwcx.onqueryGSWSZCallback",
                });
            }

        },

        onqueryGSWSZCallback: function (jsonObj) {
            curSeg.reloadPicCode(2);
            flag2 = true;
            switch (jsonObj.code) {
                case "0":
                    if ($.fn.dataTable.isDataTable('#wxlspzwcx2')) {
                        var table = $('#wxlspzwcx2').DataTable();
                        table.destroy();
                    }
                    $('#wxlspzwcx2').DataTable({
                        "data": jsonObj.data,
                        "columns": [{
                                "data": null,
                                "render": function (data, type, full, meta) {
                                    return meta.row + 1 + meta.settings._iDisplayStart;
                                }
                            }, {
                                "data": "PZZG_DM"
                            },
                            {
                                "data": "PZHM"
                            },
                            {
                                "data": "LRRQ",
                                "render": function (data, type, full, meta) {
                                    return data.substring(0, 10);
                                }
                            },
                            {
                                "data": "SWJGMC"
                            },
                            {
                                "data": "NSRMC"
                            },
                            {
                                "data": "SFZJLX_DM",
                                "render": function (data, type, full, meta) {
                                    switch (data) {
                                        case "201":
                                            SFZJHM_TYPE = "居民身份证";
                                            break;
                                        case "202":
                                            SFZJHM_TYPE = "军官证";
                                            break;
                                        case "204":
                                            SFZJHM_TYPE = "士兵证";
                                            break;
                                        case "208":
                                            SFZJHM_TYPE = "外国护照";
                                            break;
                                        case "210":
                                            SFZJHM_TYPE = "港澳居民来往内地通行证";
                                            break;
                                        case "213":
                                            SFZJHM_TYPE = "台湾居民来往大陆通行证";
                                            break;
                                        default:
                                            SFZJHM_TYPE = "中国护照"

                                    }
                                }
                            },
                            {
                                "data": "SFZJHM"
                            },
                            {
                                "data": "KJJE"
                            }
                        ],
                        "ordering": false,
                        "info": false,
                        "lengthChange": false,
                        "searching": false,
                        "paging": false
                    })
                    break;
                default:
                    layui.use('layer', function () {
                        var layer = layui.layer;
                        layer.msg(jsonObj.msg);
                    })
            }
        },

        onQueryCCS: function () {
            if (flag3 == false) {
                return false;
            }
            if (curSeg.inputIsEmpty('.wxn_ccs')) {

            } else {
                var CLPZHM = $(":input[name='ccs_CLPZHM']").val().trim();
                var DZSPHM = $(":input[name='ccs_DZSPHM']").val().trim();
                var SJJE = $(":input[name='ccs_SJJE']").val().trim();
                var rcode = $(":input[name='ccs_rcode']").val().trim();

                flag3 = false;

                $.ajax({
                    url: jsptHtp + "/web/server/platform/sscx/selCcsQuery.do",
                    type: "GET",
                    data: {
                        DZSPHM: DZSPHM,
                        CLPZHM: CLPZHM,
                        SJJE: SJJE,
                        rcode: rcode,
                    },
                    dataType: "jsonp",
                    jsonpCallback: "spzwcx.onqueryCCSCallback",
                });
            }
        },

        onqueryCCSCallback: function (jsonObj) {
            curSeg.reloadPicCode(3);
            flag3 = true;
            switch (jsonObj.code) {
                case "0":
                    if ($.fn.dataTable.isDataTable('#wxlspzwcx3')) {
                        var table = $('#wxlspzwcx3').DataTable();
                        table.destroy();
                    }
                    $('#wxlspzwcx3').DataTable({
                        "data": jsonObj.data,
                        "columns": [{
                                "data": null,
                                "render": function (data, type, full, meta) {
                                    return meta.row + 1 + meta.settings._iDisplayStart;
                                }
                            }, {
                                "data": "NSRSBH"
                            },
                            {
                                "data": "NSRMC"
                            },
                            {
                                "data": "JDH"
                            },
                            {
                                "data": "PZLX"
                            },
                            {
                                "data": null
                            },
                            {
                                "data": "KPRQ",
                                "render": function (data, type, full, meta) {
                                    return data.substring(0, 10);
                                }
                            },
                            {
                                "data": "KPR"
                            },
                            {
                                "data": "SEHJ"
                            },
                            {
                                "data": null,
                                "render": function (data, type, full, meta) {
                                    return "<a class = \"btnEdit\" data - rowindex = \"" + meta.row + "\" style='color:#1f3df7;text-decoration:underline;cursor:pointer;'>查看明细</a>";
                                }
                            }

                        ],
                        "ordering": false,
                        "info": false,
                        "lengthChange": false,
                        "searching": false,
                        "paging": false
                    })
                    break;
                default:
                    layui.use('layer', function () {
                        var layer = layui.layer;
                        layer.msg(jsonObj.msg);
                    })
            }
        },

        onQueryQGS: function () {
            if (flag4 == false) {
                return false;
            }
            if (curSeg.inputIsEmpty('.wxn_qgs')) {

            } else {
                var NSRSBH = $(":input[name='qgs_NSRSBH']").val().trim();
                var PZHM = $(":input[name='qgs_PZHM']").val().trim();
                var SJJE = $(":input[name='qgs_SJJE']").val().trim();
                var rcode = $(":input[name='qgs_rcode']").val().trim();
                var PZZG_DM_TYPE;
                switch ($('.qgs_PZZG_DM').find('input').val()) {
                    case "(121)豫地现契耕":
                        PZZG_DM_TYPE = "2411032121";
                        break;
                    case "(131)豫地现契耕":
                        PZZG_DM_TYPE = "2411032131";
                        break;
                    case "(141)豫地现契耕":
                        PZZG_DM_TYPE = "2411032141";
                        break;
                    case "(151)豫地现契耕":
                        PZZG_DM_TYPE = "2411032151";
                        break;
                    case "(161)豫地现契耕":
                        PZZG_DM_TYPE = "2411032161";
                        break;
                    case "(162)豫地现契耕":
                        PZZG_DM_TYPE = "2411032162";
                        break;
                    case "(171)豫地现房土":
                        PZZG_DM_TYPE = "2411032171";
                        break;
                    default:
                        PZZG_DM_TYPE = "2411032181"
                }

                flag4 = false;

                $.ajax({
                    url: jsptHtp + "/web/server/platform/sscx/selQsspQuery.do",
                    type: "GET",
                    data: {
                        NSRSBH: NSRSBH,
                        PZHM: PZHM,
                        SJJE: SJJE,
                        // rcode: rcode,
                        PZZG_DM: PZZG_DM_TYPE,
                        CUR_USERID: "",
                        START: "1",
                        END: "20"
                    },
                    dataType: "jsonp",
                    jsonpCallback: "spzwcx.onqueryQGSCallback",
                });
            }
        },

        onqueryQGSCallback: function (jsonObj) {
            curSeg.reloadPicCode(4);
            flag4 = true;
            switch (jsonObj.code) {
                case "0":
                    if ($.fn.dataTable.isDataTable('#wxlspzwcx4')) {
                        var table = $('#wxlspzwcx4').DataTable();
                        table.destroy();
                    }
                    $('#wxlspzwcx4').DataTable({
                        "data": jsonObj.data,
                        "columns": [{
                                "data": null,
                                "render": function (data, type, full, meta) {
                                    return meta.row + 1 + meta.settings._iDisplayStart;
                                }
                            }, {
                                "data": "NSRSBH"
                            },
                            {
                                "data": "NSRMC"
                            },
                            {
                                "data": "JDH"
                            },
                            {
                                "data": "PZLX"
                            },
                            {
                                "data": "QYH"
                            },
                            {
                                "data": "KPRQ",
                                "render": function (data, type, full, meta) {
                                    return data.substring(0, 10);
                                }
                            },
                            {
                                "data": "KPR"
                            },
                            {
                                "data": "SEHJ"
                            },
                            {
                                "data": null,
                                "render": function (data, type, full, meta) {
                                    return "<a class = \"btnEdit\" data - rowindex = \"" + meta.row + "\" style='color:#1f3df7;text-decoration:underline;cursor:pointer;'>查看明细</a>";
                                }
                            }
                        ],
                        "ordering": false,
                        "info": false,
                        "lengthChange": false,
                        "searching": false,
                        "paging": false
                    })
                    break;
                default:
                    layui.use('layer', function () {
                        var layer = layui.layer;
                        layer.msg(jsonObj.msg);
                    })
            }
        },

        //判断输入信息是否为空并提示
        inputIsEmpty: function (name) {
            var flag = false;
            $('' + name + ' input').each(function () {
                var that = this;
                if ($(this).val() == "") {
                    if ($(this).hasClass("layui-input")) {} else {
                        layui.use('layer', function () {
                            var layer = layui.layer;
                            layer.msg($(that).attr("data-name") + '不能为空');
                        })
                        flag = true;
                        return false;
                    }

                }
            })
            return flag;
        },

        //验证码
        reloadPicCode: function (id) {
            // http://jcpt.ha-n-tax.gov.cn
            $('#picCode' + id).attr("src", queryHtp + '/bsfwt/sscx/showPictureCode.do?t=' + Math.random());
        }


    }
})();